#include <SDL/SDL.h>
#include <iostream>

#include "Event.hpp"
#include "KeyboardEvent.hpp"
#include "Application.hpp"
#include "Utils.hpp"

Application *app = NULL;
SDL_Surface* screen;

void onKeyPress(const Event *e);

int main()
{
    TRACEIN;
    if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD) < 0) {
        fprintf(stderr, "Can't initialize video system\n");
        TRACEOUT;
        exit(1);
    }

    screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE); 
   
    if(!screen) {
        fprintf(stderr, "Cannot initialize screen\n");
        TRACEOUT;
        exit(1);
    }
    
    atexit(SDL_Quit);

    app = new Application();
    app->initialize();
    app->addEventListener(KeyboardEvent::KEY_UP, onKeyPress);

    std::cout << app->toString() << std::endl;

    while(app->running) {}

    delete app;
    TRACEOUT;

    return 0;
}

void onKeyPress(const Event *e)
{
    TRACEIN;
    std::cout << e->toString() << std::endl;
    app->finalize();
    TRACEOUT;
}
